import { _decorator, Component, Label, Sprite, Node, Texture2D, ImageAsset, SpriteFrame } from 'cc';
import { losChannel } from '../../app/Script/SDK/losChannel';
import { losChannelHw } from '../../app/Script/SDK/losChannelHw';

const { ccclass, property } = _decorator;


@ccclass('NativeInterAd')
export class nativeInterAd extends Component {
        @property(Label)
        title: Label | null = null;
        @property(Label)
        source: Label | null = null;
        @property(Label)
        clickBtnTxt: Label | null = null;
        @property(Sprite)
        picUri: Sprite | null = null;
        @property(Node)
        bg: Node | null = null;
        private adid: string = null;
        @property(Node)
        unqiuebg: Node = null;
        isBanner = false
        public init(adid: string, picUri: SpriteFrame, title: string, source: string, clickBtnTxt: string, isbanner = false) {
                this.node.setPosition(this.node.position.x, 100, 0)
                this.title.string = title;
                this.source.string = source;
                this.clickBtnTxt.string = clickBtnTxt;
                this.adid = adid;
                this.bg.on(Node.EventType.TOUCH_END, this.clickAd, this)

                if (isbanner) {
                        this.isBanner = true
                        console.error("是banner")
                } else {
                        losChannel.instance().getChannel().doAction('插屏一定会被点击', ['hw', (flag) => {
                                console.log("bg__________________", flag)
                                this.unqiuebg.active = flag
                        }]);
                        losChannel.instance().getChannel().doAction('有插屏', ['hw'])
                }
                this.unqiuebg.on(Node.EventType.TOUCH_END, this.clickAd, this)
                this.reportAdShow();


                this.putPic(picUri)
        }
        protected onDisable(): void {
                this.bg.off(Node.EventType.TOUCH_END, this.clickAd, this)

        }
        reportAdShow() {
                if (this.adid) {
                        (losChannel.instance().getChannel() as losChannelHw).reportAdShow(this.adid, this.isBanner);
                }
        }
        clickAd() {
                console.log("点击广告")
                this.close();
                if (this.adid) {
                        (losChannel.instance().getChannel() as losChannelHw).reportAdClick(this.adid, this.isBanner);
                }


        }
        putPic(img: SpriteFrame) {
                this.picUri.spriteFrame = img
        }
        loadPics(picUri) {
                let remoteUrl = picUri
                let type = '.jpg';
                if (remoteUrl.indexOf('.png') != -1) {
                        type = '.png';
                } else if (remoteUrl.indexOf('.jpg') != -1) {
                        type = '.jpg';
                } else if (remoteUrl.indexOf('.jpeg') != -1) {
                        type = '.jpeg';
                }
                // type = '.image'
                var self = this;
                const img = new Image();
                img.src = remoteUrl;
                img.crossOrigin = 'anonymous';
                img.onload = (e) => {
                        let texture = new Texture2D()
                        texture.image = new ImageAsset(img)
                        let _frame = new SpriteFrame()
                        _frame.texture = texture
                        self.picUri.spriteFrame = _frame
                }
        }
        public close(): void {
                console.log("关闭原生")
                this.scheduleOnce(() => {
                        this.node.destroy()
                }, 0.1)



        }
        protected onDestroy(): void {
                if (!this.isBanner) {
                        losChannel.instance().getChannel().doAction('插屏间隔', ['hw'])
                        losChannel.instance().getChannel().doAction('无插屏', ['hw'])
                }

                if (this.isBanner) {
                        setTimeout(() => {

                                (losChannel.instance().getChannel() as losChannelHw).showBanner()
                        }, 30000);
                }
        }
}


/**
 * Note: The original script has been commented out, due to the large number of changes in the script, there may be missing in the conversion, you need to convert it manually
 */
// import { privacyChannel } from "../prvScript/privacyChannel";
// import { privacyChannelHw } from "../prvScript/privacyChannelHw";
//
// /**
//  * Created by 席庆功
//  * Time: 2024/01/15.
//  */
// const { ccclass, property } = cc._decorator;
//
// @ccclass
// export class nativeInterAd extends cc.Component {
//
//     @property(cc.Label)
//     title: cc.Label = null;
//     @property(cc.Label)
//     source: cc.Label = null;
//     @property(cc.Label)
//     clickBtnTxt: cc.Label = null;
//     @property(cc.Sprite)
//     picUri: cc.Sprite = null;
//     @property(cc.Node)
//     bg: cc.Node = null;
//     private adid: string = null;
//     public init(adid: string, picUri: string, title: string, source: string, clickBtnTxt: string) {
//         // console.log('init',adid,picUri,title,source,clickBtnTxt)
//         // if(pzaChannel.instance().channel == 'hw'){
//         this.node.scale = 0.7;
//         this.node.y = 100;
//         // }
//         this.title.string = title;
//         this.source.string = source;
//         this.clickBtnTxt.string = clickBtnTxt;
//         this.adid = adid;
//         // this.clickUrl=clickUrl;
//
//         this.bg.on(cc.Node.EventType.TOUCH_END, this.clickAd, this)
//         //pzaWorldEventManager.addListener(pzaWorldEventType.REPORT_AD_SHOW, this.reportAdShow, this);
//
//         this.reportAdShow();
//         this.loadPics(picUri)
//     }
//
//     protected onDisable(): void {
//         this.bg.off(cc.Node.EventType.TOUCH_END, this.clickAd, this)
//         //pzaWorldEventManager.removeListener(pzaWorldEventType.REPORT_AD_SHOW, this.reportAdShow, this);
//     }
//
//     reportAdShow() {
//         // console.log('reportAdShow adid:',this.adid)
//         // console.log('reportAdShow clickUrl:',this.clickUrl)
//         if (this.adid) {
//             (privacyChannel.getChannel() as privacyChannelHw).reportAdShow(this.adid, this.node);
//         }
//     }
//
//     clickAd() {
//         console.log("点击广告")
//         if (this.adid) {
//             (privacyChannel.getChannel() as privacyChannelHw).reportAdClick(this.adid);
//             qg.openDeeplink({
//                 uri: this.source
//             });
//         }
//
//         this.close();
//     }
//
//     loadPics(picUri) {
//         // console.log('loadPics')
//         //remoteUrl 获取到的图片地址
//         let remoteUrl = picUri
//         let type = '.jpg';
//         if (remoteUrl.indexOf('.png') != -1) {
//             type = '.png';
//         } else if (remoteUrl.indexOf('.jpg') != -1) {
//             type = '.jpg';
//         } else if (remoteUrl.indexOf('.jpeg') != -1) {
//             type = '.jpeg';
//         }
//         // type = '.image'
//         var self = this;
//         // console.log("remoteUrl "+remoteUrl);
//         // console.log("type "+type);
//         // console.log('cc',cc)
//         // console.log('cc.asset',cc.assetManager)
//         // console.log('cc.assetManager.loadRemote',cc.assetManager.loadRemote)
//         // cc.assetManager.loadRemote(remoteUrl,{ext:type},(err, res: any)=>{
//         //     if (err) {
//         //         console.log('err :',err);
//         //         // pzakc.err('原生图片加载失败', type, err);
//         //         self.close();
//         //         return;
//         //     }
//         //     // console.log("err"+err);
//         //     console.log('setSP')
//         //     var spriteFrame=new cc.SpriteFrame(res);
//         //     self.picUri.spriteFrame=spriteFrame;
//         // });
//
//         const img = new Image();
//         img.src = remoteUrl;
//         img.crossOrigin = 'anonymous';
//         img.onload = (e) => {
//             // console.log('load',remoteUrl, img, e);
//             const texture = new cc.Texture2D();
//             texture.initWithElement(img);
//             self.picUri.spriteFrame = new cc.SpriteFrame(texture);
//         }
//     }
//
//     public close(): void {
//         console.log("关闭原生")
//         this.scheduleOnce(() => {
//             this.node.destroy()
//         }, 0.1)
//
//     }
// }
